﻿@model ConfigurationModel
@using SmartStore.Web.Framework;
@using SmartStore.AmazonPay.Models;
@using SmartStore.AmazonPay;
@{
    Layout = "";
}

<div class="plugin-actions">
	<button type="submit" name="save" class="btn btn-warning" value="save">
		<i class="fa fa-check"></i>
		<span>@T("Admin.Common.Save")</span>
	</button>
</div>

<div class="plugin-configuration">
	<div>
		<div class="alert alert-info alert-dismissible">
			<button type="button" class="close" data-dismiss="alert">&times;</button>
			@Html.Raw(@T("Plugins.Payments.AmazonPay.AdminInstruction"))
		</div>
	</div>
	<div>
		<a href="https://sellercentral-europe.amazon.com" target="_blank">
			<img src="@Url.Content("~/Plugins/SmartStore.AmazonPay/Content/branding.png")" alt="Amazon Pay" />
		</a>
	</div>
</div>

@{ Html.RenderAction("StoreScopeConfiguration", "Setting", new { area = "Admin" }); }

@using (Html.BeginForm())
{
	<table id="AmazonPayConfigTable" class="adminContent">
		<tr>
			<td colspan="2">
				<div class="admin-config-group">
					<div class="title">
						@Model.ConfigGroups.SafeGet(0)
					</div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				&nbsp;
			</td>
			<td class="adminData">
				<a href="javascript:void(0)" class="btn btn-primary register-now">
					<i class="fa fa-sign-in-alt"></i>
					<span>@T("Plugins.Payments.AmazonPay.RegisterNow")</span>
				</a>
				<button type="button" class="btn btn-secondary paste-access-data">
					<i class="far fa-clipboard"></i>
					<span>@T("Plugins.Payments.AmazonPay.PasteAccessData")</span>
				</button>
			</td>
		</tr>
		<tr>
			<td class="adminTitle"></td>
			<td class="adminData">
				<div class="alert alert-info">
					@Html.Raw(T("Plugins.Payments.AmazonPay.RegisterNote"))
				</div>
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.UseSandbox)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.UseSandbox)
				@Html.ValidationMessageFor(model => model.UseSandbox)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.SellerId)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.SellerId)
				@Html.ValidationMessageFor(model => model.SellerId)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AccessKey)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.AccessKey)
				@Html.ValidationMessageFor(model => model.AccessKey)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.SecretKey)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.SecretKey, null, new { htmlAttributes = new { autocomplete = "new-password", value = Model.SecretKey } })
				@Html.ValidationMessageFor(model => model.SecretKey)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.ClientId)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.ClientId)
				@Html.ValidationMessageFor(model => model.ClientId)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.Marketplace)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.Marketplace, Html.DropDownListFor(model => model.Marketplace, new List<SelectListItem>
				{
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.Marketplace.De"), Value = "de" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.Marketplace.Uk"), Value = "uk" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.Marketplace.Us"), Value = "us" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.Marketplace.Jp"), Value = "jp" }
				}))
				@Html.ValidationMessageFor(model => model.Marketplace)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.CurrentMerchantLoginDomains)
			</td>
			<td class="adminData form-control-plaintext">
				@if (Model.CurrentMerchantLoginDomains.Any())
				{
					foreach (var item in Model.CurrentMerchantLoginDomains)
					{
						<div>@Html.Raw(item)</div>
					}
				}
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.CurrentMerchantLoginRedirectUrls)
			</td>
			<td class="adminData form-control-plaintext">
				@if (Model.CurrentMerchantLoginRedirectUrls.Any())
				{
					foreach (var item in Model.CurrentMerchantLoginRedirectUrls)
					{
						<div>@Html.Raw(item)</div>
					}
				}
			</td>
		</tr>
		<tr class="adminSeparator">
			<td colspan="2">
				<div class="admin-config-group">
					<div class="title">@Model.ConfigGroups.SafeGet(1)</div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AuthorizeMethod)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.AuthorizeMethod, Html.DropDownListFor(model => model.AuthorizeMethod, Model.AuthorizeMethods))
				@Html.ValidationMessageFor(model => model.AuthorizeMethod)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.TransactionType)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.TransactionType, Html.DropDownListFor(model => model.TransactionType, Model.TransactionTypes))
				@Html.ValidationMessageFor(model => model.TransactionType)
			</td>
		</tr>
		<tr id="TransactionTypeWarning" class="d-none">
			<td class="adminTitle"></td>
			<td class="adminData">
				<div class="alert alert-warning">
					@Html.Raw(T("Plugins.Payments.AmazonPay.TransactionType.Warning"))
				</div>
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.SaveEmailAndPhone)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.SaveEmailAndPhone, Html.DropDownListFor(model => model.SaveEmailAndPhone, Model.SaveEmailAndPhones))
				@Html.ValidationMessageFor(model => model.SaveEmailAndPhone)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AddOrderNotes)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.AddOrderNotes)
				@Html.ValidationMessageFor(model => model.AddOrderNotes)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.InformCustomerAboutErrors)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.InformCustomerAboutErrors, 
					Html.CheckBoxFor(model => model.InformCustomerAboutErrors, new { data_toggler_for = "#InformCustomerAddErrorsContainer" }))
				@Html.ValidationMessageFor(model => model.InformCustomerAboutErrors)
			</td>
		</tr>
		<tr id="InformCustomerAddErrorsContainer">
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.InformCustomerAddErrors)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.InformCustomerAddErrors)
				@Html.ValidationMessageFor(model => model.InformCustomerAddErrors)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.DataFetching)
			</td>
			<td class="adminData">
				@Html.DropDownListFor(model => model.DataFetching, Model.DataFetchings)
				@Html.ValidationMessageFor(model => model.DataFetching)
			</td>
		</tr>
		<tr class="data-fetching data-fetching-ipn">
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.IpnUrl)
			</td>
			<td class="adminData">
				@Html.TextBoxFor(model => model.IpnUrl, new { @readonly = "readonly", @class = "form-control-plaintext" })
			</td>
		</tr>
		<tr class="data-fetching data-fetching-polling">
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.PollingMaxOrderCreationDays)
			</td>
			<td class="adminData">
				@Html.EditorFor(model => model.PollingMaxOrderCreationDays, new { postfix = T("Time.Days").Text })
				@Html.ValidationMessageFor(model => model.PollingMaxOrderCreationDays)
			</td>
		</tr>
		<tr id="DataFetchingWarning" class="d-none">
			<td class="adminTitle"></td>
			<td class="adminData">
				<div class="alert alert-warning">
					@Html.Raw(T("Plugins.Payments.AmazonPay.DataFetching.Warning"))
				</div>
			</td>
		</tr>

		<tr class="adminSeparator">
			<td colspan="2">
				<div class="admin-config-group">
					<div class="title">@Model.ConfigGroups.SafeGet(2)</div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.PayButtonColor)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.PayButtonColor, Html.DropDownListFor(model => model.PayButtonColor, new List<SelectListItem>
				{
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonColor.Gold"), Value = "Gold" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonColor.LightGray"), Value = "LightGray" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonColor.DarkGray"), Value = "DarkGray" }
				}))
				@Html.ValidationMessageFor(model => model.PayButtonColor)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.PayButtonSize)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.PayButtonSize, Html.DropDownListFor(model => model.PayButtonSize, new List<SelectListItem>
				{
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonSize.Small"), Value = "small" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonSize.Medium"), Value = "medium" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonSize.Large"), Value = "large" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonSize.Xlarge"), Value = "x-large" }
				}))
				@Html.ValidationMessageFor(model => model.PayButtonSize)
			</td>
		</tr>

		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AuthButtonType)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.AuthButtonType, Html.DropDownListFor(model => model.AuthButtonType, new List<SelectListItem>
				{
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.AuthButtonType.Login"), Value = "Login" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.AuthButtonType.LwA"), Value = "LwA" }
				}))
				@Html.ValidationMessageFor(model => model.AuthButtonType)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AuthButtonColor)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.AuthButtonColor, Html.DropDownListFor(model => model.AuthButtonColor, new List<SelectListItem>
				{
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonColor.Gold"), Value = "Gold" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonColor.LightGray"), Value = "LightGray" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonColor.DarkGray"), Value = "DarkGray" }
				}))
				@Html.ValidationMessageFor(model => model.AuthButtonColor)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AuthButtonSize)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.AuthButtonSize, Html.DropDownListFor(model => model.AuthButtonSize, new List<SelectListItem>
				{
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonSize.Small"), Value = "small" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonSize.Medium"), Value = "medium" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonSize.Large"), Value = "large" },
					new SelectListItem { Text = @T("Plugins.Payments.AmazonPay.ButtonSize.Xlarge"), Value = "x-large" }
				}))
				@Html.ValidationMessageFor(model => model.AuthButtonSize)
			</td>
		</tr>

		<tr class="adminSeparator">
			<td colspan="2">
				<div class="admin-config-group">
					<div class="title">@Model.ConfigGroups.SafeGet(3)</div>
				</div>
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.ShowPayButtonForAdminOnly)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.ShowPayButtonForAdminOnly)
				@Html.ValidationMessageFor(model => model.ShowPayButtonForAdminOnly)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.ShowButtonInMiniShoppingCart)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.ShowButtonInMiniShoppingCart)
				@Html.ValidationMessageFor(model => model.ShowButtonInMiniShoppingCart)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AdditionalFee)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.AdditionalFee, null, new { postfix = Model.PrimaryStoreCurrencyCode })
				@Html.ValidationMessageFor(model => model.AdditionalFee)
			</td>
		</tr>
		<tr>
			<td class="adminTitle">
				@Html.SmartLabelFor(model => model.AdditionalFeePercentage)
			</td>
			<td class="adminData">
				@Html.SettingEditorFor(model => model.AdditionalFeePercentage)
				@Html.ValidationMessageFor(model => model.AdditionalFeePercentage)
			</td>
		</tr>
	</table>
}

@* Must not be the first form in this view. *@
<form id="register-now-form" method="POST" action="@Model.RegisterUrl" target="_blank">
	<input type="hidden" name="spId" value="@Model.PlatformId" />

	@*Not available for europe at the moment:*@
	@*<input type="hidden" name="publicKey" value="@Model.PublicKey" />*@

	<input type="hidden" name="keyShareURL" value="@Model.KeyShareUrl" />
	<input type="hidden" name="locale" value="@Model.LanguageLocale" />
	<input type="hidden" name="merchantCountry" value="@Model.MerchantCountry" />
	@foreach (var item in Model.MerchantLoginDomains)
	{
		<input type="hidden" name="merchantLoginDomains[]" value="@item" />
	}
	<input type="hidden" name="spSoftwareVersion" value="@Model.SoftwareVersion" />
	<input type="hidden" name="spAmazonPluginVersion" value="@Model.PluginVersion" />
	<input type="hidden" name="ld" value="@Model.LeadCode" />
	@foreach (var item in Model.MerchantLoginRedirectUrls)
	{
		<input type="hidden" name="merchantLoginRedirectURLs[]" value="@item" />
	}
	<input type="hidden" name="merchantPrivacyNoticeURL" value="@Model.MerchantPrivacyNoticeUrl" />
	<input type="hidden" name="merchantStoreDescription" value="@Model.MerchantStoreDescription" />
	<input type="hidden" name="merchantSandboxIPNURL" value="@Model.MerchantSandboxIpnUrl" />
	<input type="hidden" name="merchantProductionIPNURL" value="@Model.MerchantProductionIpnUrl" />
	<input type="hidden" name="source" value="SPPL" />
</form>

@{Html.SmartStore().Window()
	.Name("register-now-dialog")
	.Title(T("Plugins.Payments.AmazonPay.PasteAccessData"))
	.Content(
	@<text>
		<form id="access-data-form" method="POST" action="@Url.Action("SaveAccessData", "AmazonPay", new { area = AmazonPayPlugin.SystemName })">
			<textarea id="amazon-access-data" name="accessData" rows="12" class="form-control" style="width: 98%;" placeholder="@T("Plugins.Payments.AmazonPay.PasteAccessData.Hint")"></textarea>
		</form>
	</text>)
	.FooterContent(
	@<text>
		<button class="btn btn-secondary btn-flat" data-dismiss="modal">
			<span>@T("Common.Cancel")</span>
		</button>
		<button id="amazon-save-access-data" type="submit" class="btn btn-primary" disabled="disabled">
			<span>@T("Plugins.Payments.AmazonPay.SaveAccessData")</span>
		</button>
	</text>)
	.Render();
}

<script type="text/javascript">
	jQuery(document).ready(function () {

		// Show/hide transaction type warning
		$('#@(Html.FieldIdFor(x => x.TransactionType))').change(function () {
			$('#TransactionTypeWarning').toggleClass('d-none', $(this).val() !== '2');
		}).trigger('change');

		// Show/hide status fetching warning
		$('#@(Html.FieldIdFor(x => x.DataFetching))').change(function () {
			var val = $(this).val(),
				configTable = $('#AmazonPayConfigTable');

			$('#DataFetchingWarning').toggleClass('d-none', val !== '1');

			configTable.find('.data-fetching').hide();
			if (val === '1') {
				configTable.find('.data-fetching-ipn').show();
			}
			else if (val === '2') {
				configTable.find('.data-fetching-polling').show();
			}
		}).trigger('change');

		// Register now
		$('.register-now').click(function () {
			$('.paste-access-data').trigger('click');
			$('#register-now-form').submit();
		});

		// Save access data
		$('#amazon-save-access-data').click(function () {
			$('#access-data-form').submit();
		});

		// Paste access data
		$('.paste-access-data').click(function () {
			//$('#amazon-access-data').trigger('change');
			$('#register-now-dialog').modal('show');
			
			setTimeout(function () {
				$('#amazon-access-data').focus();
			}, 800);
		});

		// Keyup in access data textarea
		$('#amazon-access-data').keyup(function () {
			$('#register-now-dialog').find('.btn-primary').prop('disabled', $(this).val().length == 0);
		});
	});
</script>